import { createContext, useContext } from "react";
import validators from "./validators";
import { Validator } from "./Validator";
import { Controller } from "./Controller";

interface IInitContext {
    validators: Record<string, typeof Validator<any>>;
    controller?: Controller;
}

export const ValidationContext = createContext<IInitContext>({
    validators
});

export const useValidation = () => {
    const { controller } = useContext(ValidationContext);
    if(!controller) {
        throw new Error("在使用useValidation前须先初始化ValidationProvider组件");
    }
    return controller;
};


